package com.telecom.data; import java.io.BufferedWriter; import java.io.FileWriter; 
import java.io.IOException; import java.text.SimpleDateFormat; import 
java.util.ArrayList; import java.util.List; import java.util.Random; import 
java.util.Calendar; public class ProducerSimulator {
    private static final int RECORD_COUNT = 10000; // 生成记录数
    private static final String OUTPUT_FILE = "telecom_data.csv"; // 输出文件
    // 随机生成手机号码
    private static String generatePhoneNumber() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        // 中国手机号以1开头，第二位通常是3-9
        sb.append("1");
        sb.append(3 + random.nextInt(7));
        for (int i = 0; i < 9; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
    // 随机生成通话时间(过去一年内的随机时间)
    private static String generateCallTime() {
        Calendar calendar = Calendar.getInstance();
        // 随机减去0-365天
        calendar.add(Calendar.DAY_OF_YEAR, -new Random().nextInt(365));
        // 随机小时和分钟
        calendar.set(Calendar.HOUR_OF_DAY, new Random().nextInt(24));
        calendar.set(Calendar.MINUTE, new Random().nextInt(60));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(calendar.getTime());
    }
    // 随机生成通话时长(1-1200秒)
    private static int generateDuration() {
        return 1 + new Random().nextInt(1200);
    }
    public static void main(String[] args) {
        try (BufferedWriter writer = new BufferedWriter(new 
FileWriter(OUTPUT_FILE))) {
            // 写入CSV文件头
            writer.write("caller,callee,call_time,duration\n");
            // 生成100个常用号码
            List<String> phoneNumbers = new ArrayList<>();
            for (int i = 0; i < 100; i++) {
                phoneNumbers.add(generatePhoneNumber());
            }
            // 生成通话记录
            for (int i = 0; i < RECORD_COUNT; i++) {
                // 随机选择主叫和被叫号码
                String caller = phoneNumbers.get(new 
Random().nextInt(phoneNumbers.size()));
                String callee;
                do {
                    callee = phoneNumbers.get(new 
Random().nextInt(phoneNumbers.size()));
                } while (caller.equals(callee)); // 确保主叫和被叫不同
                String callTime = generateCallTime();
                int duration = generateDuration();
                // 写入记录
                writer.write(String.format("%s,%s,%s,%d\n", caller, callee, 
callTime, duration));
            }
            System.out.println("成功生成" + RECORD_COUNT + "条通话记录到" + 
OUTPUT_FILE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
